﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

using WebSite.Domain;
using System.Data;

namespace WebSite.DAL {
    public class PacienteDAO {

        public static int cadastrarPacientes(Paciente paciente) {
            string strsql = @"INSERT INTO tab_DadosPaciente (nome,CPF) VALUES (@Nome,@CPF)
                            SELECT scope_identity() idDadosPaciente;";

            int idPaciente = 0;
            using (SqlConnection objConnection = ConnectionFactory.obterConexao()) {
                objConnection.Open();
                using (SqlTransaction objTransaction = objConnection.BeginTransaction(IsolationLevel.Serializable)) {
                    using (SqlCommand objCommand = new SqlCommand(strsql, objConnection, objTransaction)) {
                        objCommand.Parameters.Add(new SqlParameter("@Nome", paciente.nome));
                        objCommand.Parameters.Add(new SqlParameter("@CPF", paciente.cpf));

                        idPaciente = Convert.ToInt32(objCommand.ExecuteScalar());
                        objTransaction.Commit();
                    }
                }
            }
            return idPaciente;
        }

        //futuramente passar idMedico para buscar Pacientes do Médico
        public static void BuscarPacientes(ListItemCollection li)//(Medico medico)
        {
            SqlDataReader dr;
            string strsql = "Select * from tab_DadosPaciente";
            SqlConnection objConnection = ConnectionFactory.obterConexao();            

            try
            {
                objConnection.Open();
                SqlCommand objCommand = new SqlCommand(strsql, objConnection);
                dr = objCommand.ExecuteReader();

                while (dr.Read())
                {
                    li.Add (new ListItem(dr["nome"].ToString(), dr["idDadosPaciente"].ToString()));
                    
                }

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {                
                objConnection.Close();
            }            
        }
    }
}